package com.micro.train.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.micro.train.entity.Train;
import com.micro.train.repository.TrainRepository;
import com.micro.train.service.TrainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 火车信息服务实现类
 */
@Service
public class TrainServiceImpl extends ServiceImpl<TrainRepository, Train> implements TrainService {
    
    @Autowired
    private TrainRepository trainRepository;
    
    @Override
    public Train addTrain(Train train) {
        // 检查车次是否已存在
        if (existsByTrainNumber(train.getTrainNumber())) {
            throw new RuntimeException("车次已存在：" + train.getTrainNumber());
        }
        trainRepository.insert(train);
        return train;
    }
    
    @Override
    public Train updateTrain(Long trainId, Train train) {
        Train existingTrain = getTrainById(trainId);
        if (existingTrain == null) {
            throw new RuntimeException("列车不存在，ID：" + trainId);
        }
        
        // 如果修改了车次号，需要检查是否与其他车次冲突
        if (!existingTrain.getTrainNumber().equals(train.getTrainNumber()) && 
            existsByTrainNumber(train.getTrainNumber())) {
            throw new RuntimeException("车次已存在：" + train.getTrainNumber());
        }
        
        train.setTrainId(trainId);
        trainRepository.updateById(train);
        return train;
    }
    
    @Override
    public void deleteTrain(Long trainId) {
        Train train = getTrainById(trainId);
        if (train == null) {
            throw new RuntimeException("列车不存在，ID：" + trainId);
        }
        trainRepository.deleteById(trainId);
    }
    
    @Override
    public List<Train> getAllTrains() {
        return trainRepository.selectList(null);
    }
    
    @Override
    public Train getTrainById(Long trainId) {
        return trainRepository.selectById(trainId);
    }
    
    @Override
    public Train getTrainByNumber(String trainNumber) {
        return trainRepository.selectByTrainNumber(trainNumber);
    }
    
    @Override
    public List<Train> getTrainsByType(String trainType) {
        return trainRepository.selectByTrainType(trainType);
    }
    
    @Override
    public List<Train> getTrainsByDepartureStation(Long departureStationId) {
        return trainRepository.selectByDepartureStationId(departureStationId);
    }
    
    @Override
    public List<Train> getTrainsByTerminalStation(Long terminalStationId) {
        return trainRepository.selectByTerminalStationId(terminalStationId);
    }
    
    @Override
    public List<Train> getTrainsByStatus(Integer status) {
        return trainRepository.selectByStatus(status);
    }
    
    @Override
    public List<Train> getTrainsByRoute(Long departureStationId, Long terminalStationId) {
        return trainRepository.selectByRoute(departureStationId, terminalStationId);
    }
    
    @Override
    public boolean existsByTrainNumber(String trainNumber) {
        return trainRepository.existsByTrainNumber(trainNumber);
    }
    
    @Override
    public Long getTrainIdByTrainNumber(String trainNumber) {
        return trainRepository.selectTrainIdByTrainNumber(trainNumber);
    }
}
